﻿namespace Cuce.ReportingServices
{
    using System;
    using System.Text;

    using SSRSWS;
    
    /// <summary>
    /// 
    /// </summary>
    public class ReportingServiceHelper
    {

        /// <summary>
        /// Uploads the report.
        /// </summary>
        /// <param name="reportingService">The reporting service.</param>
        /// <param name="reportName">Name of the report.</param>
        /// <param name="newReportXML">The new report XML.</param>
        /// <returns></returns>
        public static Warning[] UploadReport(IReportingService reportingService, ReportName reportName, string newReportXML)
        {
            if (reportName == null)
                throw new ArgumentException("Report Name is null", "reportName");

            if (string.IsNullOrEmpty(newReportXML))
                throw new ArgumentException("New Report XML not set", "newReportXML");

            if (reportingService == null)
                throw new ArgumentException("Reporting Service is null", "reportingService");

            
            Warning[] warnings ;

            var encoding = new ASCIIEncoding();

            byte[] definition = encoding.GetBytes(newReportXML);

            try
            {
                warnings = reportingService.CreateReport(reportName.Name, reportName.Folder, true, definition, null);
            }
            catch (Exception e)
            {
                throw new ReportException("Web Exception for Create Report", e);
            }

            //if (warnings != null)
            //{
            //    throw new ReportException("Warnings for Create Report", warnings);
            //}

            return warnings;

        }


        /// <summary>
        /// Deletes the report.
        /// </summary>
        /// <param name="reportingService">The reporting service.</param>
        /// <param name="reportName">Name of the report.</param>
        public static void DeleteReport(IReportingService reportingService, ReportName reportName)
        {

            if (reportName == null)
                throw new ArgumentException("Report Name is null", "reportName");

            if (reportingService == null)
                throw new ArgumentException("Reporting Service is null", "reportingService");

            try
            {
                reportingService.DeleteItem(reportName.ToString());
            }
            catch (Exception e)
            {
                throw new ReportException("Web Exception for Create Report", e);

            }
        }


        /// <summary>
        /// Downloads the report.
        /// </summary>
        /// <param name="reportingService">The reporting service.</param>
        /// <param name="reportName">Name of the report.</param>
        /// <returns></returns>
        public static string DownloadReport(IReportingService reportingService, ReportName reportName)
        {

            if (reportName == null)
                throw new ArgumentException("Report Name is null", "reportName");

      
            if (reportingService == null)
                throw new ArgumentException("Reporting Service is null", "reportingService");

            try
            {
                byte[] results = reportingService.GetReportDefinition(reportName.ToString());
                
                return new ASCIIEncoding().GetString(results);
            
            }
            catch (Exception e)
            {
                throw new ReportException("Web Exception for Create Report", e);

            }

        }
    }
}
